package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.SystemCode;
import com.hl.sfzy.entity.Consult;
import com.hl.sfzy.entity.ConsultField;
import com.hl.sfzy.service.ConsultFieldService;
import com.hl.sfzy.service.ConsultService;
import com.hl.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ConsultFieldController
 * @Description WHATETODO
 * @Author Wxx
 * @Date 2024年01月15日 20:43
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/consultField")
public class ConsultFieldController {
    @Autowired
    private ConsultFieldService consultFieldService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody ConsultField consultField) throws Exception {
        consultFieldService.add(consultField);
        return Result.success();
    }

    /**
     * 编辑未使用
     */
    @PutMapping("/edit")
    public com.hl.sfzy.utils.Result edit(@RequestBody ConsultField consultField) {
        consultFieldService.edit(consultField);
        return com.hl.sfzy.utils.Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        consultFieldService.remove(id);
        return com.hl.sfzy.utils.Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        System.out.println(ids);
        consultFieldService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("/find/{id}")
    public com.hl.sfzy.utils.Result findById(@PathVariable Integer id) {
        return com.hl.sfzy.utils.Result.success(consultFieldService.findById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/find")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                                             @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                                             ConsultField consultField) {
        return Result.success(consultFieldService.findPage(pageNum, pageSize, consultField));
    }

    /**
     * 判断用户名是否重复
     */
    @GetMapping("/findExists/{name}")
    public Result findUsernameExists(@PathVariable String name) {
        ConsultField consultField = consultFieldService.selectByName(name);
        if (consultField == null) {
            return Result.success();
        }
        return Result.error(SystemCode.USERNAME_EXISTS.getCode(), SystemCode.USERNAME_EXISTS.getMessage(), null);
    }
}